﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ISS.Models;
using ISS.Business;

namespace ISS.Controllers
{
    public class AlbumController : Controller
    {
        //
        // GET: /Album/

        public ActionResult Index()
        {
            Users u = General.CheckLogin();
            if(u != null)
            {
                IEnumerable<Album> albums = new AlbumDAO().GetByUser(u.UserId);
                try
                {
                    Session["Album"] = albums.Where(a => a.AlbumName == "Untitled").First();
                }
                catch (Exception ex)
                {
                }
                return View(albums);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [HttpPost]
        public ActionResult Create(FormCollection forms)
        {
            Users user = General.CheckLogin();
            String name = forms["AlbumName"];
            Album album = new Album()
            {
                AlbumAlias = General.ConvertToNoSign(name),
                AlbumName = name,
                CommentCount = 0,
                CreateDate = DateTime.Now,
                Creator = user.UserId,
                EmbedHtml = "",
                ImageCount = 0,
                NumbersOfImage = 0,
                Permission = 0,
                Like = 0,
                Tags = "",
                Thumbnail = "",
                ViewCount = 0
            };
            if (new AlbumDAO().Create(album) > 0)
            {
                return RedirectToAction("MyAlbum", "Image", new {ID = album.AlbumId});
            }
            else
            {
                return View("Index");
            }
            return View();
        }

    }
}
